import { AfterViewInit, Component, Inject, OnInit, ViewChild, ElementRef } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { AuthService } from '@core/auth/auth.service';
import { CustomerConfigService } from '@app/core/public-api';
import { MAT_DIALOG_DATA } from '@angular/material/dialog';

@Component({
    selector: 'single-video-play',
    templateUrl: './single-video-play.component.html',
    styleUrls: ['./single-video-play.component.scss']
})
export class SingleVideoPlayComponent implements OnInit, AfterViewInit {

    @ViewChild('video')
    video: ElementRef<HTMLVideoElement> = null;
    closevideo() {
        const flvjs = (window as any).flvjs;
        window['andy'] = this.flvPlayer
        this.flvPlayer.destroy()
    }
    videoUrl = ''
    flvPlayer: any;
    constructor(private http: HttpClient, @Inject(MAT_DIALOG_DATA) public data: { cameraId: string }, private customerconfigService: CustomerConfigService) {
        let orgcode=this.customerconfigService.getConfig().orgConfig.tenant.title
        this.http.post('/api/tenant_ip/getRtsp', { cameraId: data.cameraId, minute: 0 ,orgcode})
            .subscribe(e => {
                const response = e as any;
                const flvjs = (window as any).flvjs;
                let url = `/api/flv/get_flv?url=${response.result}`;
                console.log(response);
                console.log(url);
                this.videoUrl = url;
                this.flvPlayer = flvjs.createPlayer({
                    type: 'flv',
                    url: url,
                }, {
                    headers: {
                        "X-Authorization": "Bearer " + AuthService.getJwtToken()
                    }
                });
                this.flvPlayer.attachMediaElement(this.video.nativeElement);
                this.flvPlayer.load();
            });

    }
    ngAfterViewInit(): void {

    }

    ngOnInit(): void {


    }


}
